﻿/*
创建人：zhunan
日期：2022-07-15
描述：工装批次关联订单js
*/
var page = {
  pagetotal: 0,
  pageSize: 12,
  pageNumber: 1,
  mfgorderQueryInfo: {
    pageNumber: 1,
    pageSize: 12,
    pagetotal: 0
  },
  mainUrl: getRootPath() + "/Ashx/GongZhuangContainer/GZContainerMfgInfoMaint.ashx", //本页面后台路径
  otherHandler: getRootPath() + "/Ashx/ApsHandler/EmployeeMaintPage.ashx",
  currentUserInfo: {},
  //初始化页面
  init: function () {
    page.bind();
    page.initData(); //初始化页面数据
  },
  // 页面事件绑定
  bind: function () {
    // 批次信息
    $("#tbContainer").datagrid({
      columns: [[
        { field: 'containerid', title: '批次id', hidden: true },
        { field: 'productid', title: '件号id', hidden: true },
        { field: 'mfgordername', title: '生产订单号', width: 120 },
        { field: 'hasmfgorder', title: '管理状态', width: 90 },
        { field: 'containername', title: '批次号', width: 160 },
        {
          field: 'factorystartdate', title: '创建时间', width: 85,
          formatter: function (value, row, index) {
            if (value) {
              return value.split('T')[0];
            } else {
              return value;
            }
          }
        },
        { field: 'description', title: '零件名称', width: 100 },
        { field: 'qty', title: '数量', width: 40 },
        { field: 'fullname', title: '开卡人', width: 60 },
        { field: 'workcentername', title: '工区', width: 100 },
        { field: 'factoryname', title: '分厂', width: 100 },
        { field: 'mainorsoncontainer', title: '主子卡', width: 60 }
      ]],
      onLoadSuccess: function (data) {
        $('#pp').pagination('refresh', {	// 改变选项，并刷新分页栏信息
          total: page.pagetotal,
          pageSize: page.pageSize,
          pageNumber: page.pageNumber
        });
        customizedEasyuiPagination('pp'); //定制化分页
      },
      //行点击事件
      onClickRow: function (rowIndex, rowData) {
        page.searchMfg(rowData);
      }
    });

    //批次表格分页
    $('#pp').pagination({
      onSelectPage: function (pageNumber, pageSize) {
        customizedEasyuiPagination('pp'); //定制化分页
        page.pageSize = pageSize;
        page.pageNumber = pageNumber;
        page.search();
      }
    });

    //绑定分页go按钮
    $(function () {
      $('#pageGo').on("click", function () {
        var pageNumber = $(this).parents('tr').find('.pagination-num').val();
        page.pageNumber = pageNumber;
        page.search();
      });
    });
    //  订单表格
    $("#tbMfgorder").datagrid({
      columns: [[
        { field: 'ck', title: '选择', checkbox: true },
        { field: 'mfgorderid', title: '批次id', hidden: true },
        { field: 'mfgordername', title: '生产订单号', width: 160 },
        { field: 'ordertypename', title: '订单类型', width: 60 },
        { field: 'qty', title: '订单数', width: 60 },
        { field: 'factoryname', title: '分厂', width: 120 },
        { field: 'productname', title: '零件图号', width: 140 },
        { field: 'description', title: '零件名称', width: 140 }
      ]],
      onLoadSuccess: function (data) {
        $('#mfgPaging').pagination('refresh', {	// 改变选项，并刷新分页栏信息
          total: page.mfgorderQueryInfo.pagetotal,
          pageSize: page.mfgorderQueryInfo.pageSize,
          pageNumber: page.mfgorderQueryInfo.pageNumber
        });
        customizedEasyuiPagination('mfgPaging'); //定制化分页
      }
    });

    // 订单列表分页
    $('#mfgPaging').pagination({
      onSelectPage: function (pageNumber, pageSize) {
        customizedEasyuiPagination('mfgPaging'); //定制化分页
        page.mfgorderQueryInfo.pageSize = pageSize;
        page.mfgorderQueryInfo.pageNumber = pageNumber;
        page.searchMfg();
      }
    });

    // 订单列表分页go按钮
    $(function () {
      $('#mfgPageGo').on("click", function () {
        var pageNumber = $(this).parents('tr').find('.pagination-num').val();
        page.mfgorderQueryInfo.pageNumber = pageNumber;
        page.searchMfg();
      });
    });
    $("#btnSearch").on("click", function () {
      page.pageNumber = 1;
      page.search();
    });

    // 保存
    $("#btnSave").on("click", function () {
      var datas = $("#tbContainer").datagrid('getChecked');
      var selectedMfgorder = $("#tbMfgorder").datagrid('getChecked');
      if (datas.length === 0) {
        displayMessage("请勾选要添加关联的批次！", false);
        return;
      }
      if (datas[0].hasmfgorder === '已关联') {
        displayMessage("请先解除关联，然后再试", false);
        return;
      }
      if (selectedMfgorder.length === 0) {
        displayMessage("请勾选订单", false);
        return;
      }
      if (selectedMfgorder.length >= 2) {
        displayMessage("不能同时勾选多条订单！", false);
        return;
      }
      page.saveContinerMfgInfo()
    });

    // 解除关联
    $("#btnDel").on("click", function () {
      var datas = $("#tbContainer").datagrid('getChecked');
      if (datas.length === 0) {
        displayMessage("请勾选要解除关联的记录！", false);
        return;
      }
      if (datas[0].hasmfgorder === '未关联') {
        displayMessage("批次未关联订单，不能解除关联", false);
        return;
      }
      page.delContainerMfgInfo()
    });
    //订单导出
    $("#btnExport").on("click", function () {
      var formData = {
        action: "ExportMfgorder",
        mfgordername: $("#txtMfgorder").val(),
        productname: $("#txtProduct").val(),
        containername: $("#txtContainer").val(),
        factoryid: $('#ddlFactory').combobox('getValue'),
        workcenterid: $('#ddlWorkcenter').combobox('getValue'),
        starter: $("#txtContainerStarter").textbox('getValue'),
        startdateBegin: $('#startDateBegin').val(),
        startdateEnd: $('#startDateEnd').val(),
        status: $('#ddlStatus').combobox('getValue')
      };
      $.ajax({
        type: "Post",
        url: page.mainUrl,
        data: formData,
        cashe: false,
        async: true,
        success: function (data) {
          var result = JSON.parse(data);
          if (result.Result) {
            //window.open(getRootPath()+result.ReturnData);
            var elemIF = document.createElement("iframe");
            elemIF.src = getRootPath() + result.ReturnData;
            elemIF.style.display = "none";
            document.body.appendChild(elemIF);
          } else {
            displayMessage(result.Message, false);
          }
        }
      });
    });

  },
  //初始化页面
  initData: function () {
    page.getFactory();
    page.getCurrentUserInfo();
  },
  // 查询
  search: function (pageNumber) {
    $('#tbMfgorder').datagrid('loadData', []);
    var formData = {
      action: "GetContainerCurrentInfo",
      pageSize: page.pageSize,
      pageNumber: pageNumber || page.pageNumber,
      mfgordername: $("#txtMfgorder").val(),
      productname: $("#txtProduct").val(),
      containername: $("#txtContainer").val(),
      factoryid: $('#ddlFactory').combobox('getValue'),
      workcenterid: $('#ddlWorkcenter').combobox('getValue'),
      starter: $("#txtContainerStarter").textbox('getValue'),
      startdateBegin: $('#startDateBegin').val(),
      startdateEnd: $('#startDateEnd').val(),
      status: $('#ddlStatus').combobox('getValue')
    };

    $.ajax({
      type: "Post",
      url: page.mainUrl + "?actions=GetContainerCurrentInfo",
      data: formData,
      cashe: false,
      async: true,
      beforeSend: function () {
        $('#tbContainer').datagrid('loading');
        $(this).attr("disabled", "disabled"); //禁用按钮
      },
      success: function (data) {
        $('#tbContainer').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        var result = JSON.parse(data);
        if (result.Result) {
          //给分页对象赋值
          page.pagetotal = result.RowCount;
          $('#tbContainer').datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
        } else {
          displayMessage(result.Message, false);
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        $('#tbContainer').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        alert(XMLHttpRequest.responseText);
      }
    });
  },
  searchMfg: function (rowData) {
    //page.reset(true, true);
    $('#tbMfgorder').datagrid('loadData', []);
    // 已关联批次不查询订单
    if (rowData && rowData.hasmfgorder == '已关联') {
      return
    }
    var formData = {
      action: "GetMfgorder",
      pageSize: page.mfgorderQueryInfo.pageSize,
      pageNumber: page.mfgorderQueryInfo.pageNumber,
      productname: rowData.productname,
      factoryid: $("#ddlFactory").combobox("getValue")
    };
    $.ajax({
      type: "Post",
      url: page.mainUrl + "?actions=GetMfgorder",
      data: formData,
      cashe: false,
      async: true,
      beforeSend: function () {
        $('#tbMfgorder').datagrid('loading');
        $(this).attr("disabled", "disabled"); //禁用按钮
      },
      success: function (data) {
        $('#tbMfgorder').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        var result = JSON.parse(data);
        if (result.Result) {
          //给分页对象赋值
          page.mfgorderQueryInfo.pagetotal = result.RowCount;
          $('#tbMfgorder').datagrid('loadData', { total: result.RowCount, rows: result.DBTable });
        } else {
          displayMessage(result.Message, false);
        }
      },
      error: function (XMLHttpRequest, textStatus, errorThrown) {
        $('#tbMfgorder').datagrid('loaded');
        $(this).removeAttr("disabled"); //恢复按钮
        alert(XMLHttpRequest.responseText);
      }
    });
  },
  // 清除方法，重要！！！
  reset: function (iscondition, isresult) {
    clearMessage();
    if (isresult) {
      clearEasyuiDataGrid("tbContainer", 1);
    }
  },
  // 获取分厂列表
  getFactory: function () {
    $.ajax({
      type: "post",
      url: page.otherHandler + "?actions=getFactory",
      data: { action: "GetFactory" },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          //绑定分厂
          res.DBTable.unshift({ factoryid: '', factoryname: '' });
          $("#ddlFactory").combobox({
            valueField: 'factoryid',
            textField: 'factoryname',
            panelHeight: 200,
            data: res.DBTable,
            onSelect: function (record) {
              if (record.factoryid) {
                page.bindWorkcenter(record.factoryid);// 加载工区
              }
            }
          });
          // $("#ddlFactory").combobox('select', '');
        } else {
          displayMessage(res.Message, res.Result);
        }
      }
    });
  },
  // 绑定工区
  bindWorkcenter: function (factoryid) {
    $.ajax({
      url: page.otherHandler + "?actions=getWorkcenterList",
      data: { action: "GetWorkcenter", factoryid: factoryid },
      type: "post",
      cashe: false,
      dataType: 'json',
      success: function (res) { //ajax成功后
        if (res.Result) {
          //绑定工区
          res.DBTable.unshift({ workcenterid: '', workcentername: '' });
          $("#ddlWorkcenter").combobox({
            valueField: 'workcenterid',
            textField: 'workcentername',
            panelHeight: 200,
            data: res.DBTable,
          });
          $("#ddlWorkcenter").combobox('select', '');
        } else {
          displayMessage(res.Message, res.Result);
        }
      }
    });
  },
  // 获取当前登录用户
  getCurrentUserInfo: function () {
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=GetCurrentUserInfo",
      data: { action: "GetCurrentUserInfo" },
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.ReturnData) {
          page.currentUserInfo = res.ReturnData;
          $("#ddlFactory").combobox('select', page.currentUserInfo.FactoryID);
          $("#ddlFactory").attr("disabled", "disabled");
          $("#txtContainerStarter").textbox('setValue', res.ReturnData.FullName)
        } else {
          displayMessage(res.Message, res.Result);
        }
      }
    });
  },
  // 添加关联
  saveContinerMfgInfo: function () {
    var container = $("#tbContainer").datagrid('getChecked');
    var mfgorder = $("#tbMfgorder").datagrid('getChecked');
    var data = {
      action: "SaveContinerMfgInfo",
      containerid: container[0].containerid,
      mfgorderid: mfgorder[0].mfgorderid,
      mfgordername: mfgorder[0].mfgordername,
      planQty: mfgorder[0].qty
    };
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=SaveContinerMfgInfo",
      data: data,
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage("添加关联成功!", true);
          $('#tbMfgorder').datagrid('loadData', []);
          page.search(1);
        } else {
          displayMessage(res.Message, res.Result);
        }
      }
    });
  },
  // 解除关联
  delContainerMfgInfo: function () {
    var container = $("#tbContainer").datagrid('getChecked');
    var data = {
      action: "DelContinerMfgInfo",
      containerid: container[0].containerid
    };
    $.ajax({
      type: "post",
      url: page.mainUrl + "?actions=DelContinerMfgInfo",
      data: data,
      cashe: false,
      dataType: 'json',
      success: function (res) {
        if (res.Result) {
          displayMessage("解除关联成功!", true);
          page.search(1);
        } else {
          displayMessage(res.Message, res.Result);
        }
      }
    });
  }
};
page.init();